package com.breaktian.share;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.foundation_share.R;

import java.util.List;

public class ShareDialog extends Dialog implements View.OnClickListener {

    private Share mShare;

    private List<ShareChannel> mChannels;

    ShareDialog(@NonNull Context context, Share share, List<ShareChannel> channels) {
        super(context);
        mShare = share;
        mChannels = channels;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_share);

        Window window = getWindow();
        if (window != null) {
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.gravity = Gravity.BOTTOM;
            lp.width = getContext().getResources().getDisplayMetrics().widthPixels;
            window.setAttributes(lp);

            window.setBackgroundDrawable(new ColorDrawable(0));
        }
        setCanceledOnTouchOutside(false);

        LinearLayout layout = (LinearLayout) findViewById(R.id.shareContainer);
        for (ShareChannel channel : mChannels) {
            layout.addView(itemView(channel));
        }
        findViewById(R.id.shareCancelBtn).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.shareCancelBtn) {
            mShare.cancel();
            dismiss();
        }
    }

    @Override
    public void show() {
        try {
            super.show();
        } catch (Exception ignore) {}
    }

    @Override
    public void dismiss() {
        try {
            super.dismiss();
        } catch (Exception ignore) {}
    }


    private View itemView(ShareChannel channel) {
        ShareItemView itemView = new ShareItemView(getContext(), channel);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lp.leftMargin = (int) (getContext().getResources().getDisplayMetrics().density * 15);
        itemView.setLayoutParams(lp);
        itemView.setOnClickListener(new ItemClick(channel));
        return itemView;
    }

    class ShareItemView extends LinearLayout {
        public ShareItemView(Context context, ShareChannel channel) {
            super(context);
            LayoutInflater.from(context).inflate(R.layout.item_share, this);

            ImageView imageView = (ImageView) findViewById(R.id.itemImg);
            TextView textView = (TextView) findViewById(R.id.itemText);

            imageView.setImageResource(channel.drawable());
            textView.setText(channel.getName());
        }
    }

    class ItemClick implements View.OnClickListener {
        private ShareChannel mChannel;

        public ItemClick(ShareChannel channel) {
            this.mChannel = channel;
        }

        @Override
        public void onClick(View v) {
            mShare.share(mChannel);
            dismiss();
        }
    }
}
